package cn.tedu.channel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelDemo {

    public static void main(String[] args) throws IOException {

        // 获取Channel对象
        FileChannel fc =
                new RandomAccessFile("D:\\a.txt", "rw").getChannel();

        // 写出数据
        // fc.write(ByteBuffer.wrap("test fileChannel".getBytes()));

        // 读取数据
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        fc.read(buffer);
        System.out.println(new String(buffer.array(), 0, buffer.position()));

        // 关流
        fc.close();

    }

}
